package com.LeeCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 可见点的最大数目
 */

public class Code1610 {
    public static void main(String[] args) {
        System.out.println(Math.atan2(0, -1) * 180 / Math.PI);
    }

    public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
        int posx = location.get(0);
        int posy = location.get(1);
        List<Double> angles = new ArrayList<>();
        int same = 0; // 记录与location重合的点数

        for (List<Integer> point : points) {
            int x = point.get(0);
            int y = point.get(1);
            if (x == posx && y == posy) {
                same++;
                continue;
            }
            double theta = Math.atan2(y - posy, x - posx) * 180 / Math.PI;
            angles.add(theta < 0 ? theta + 360 : theta); // 转换为[0, 360)
        }

        Collections.sort(angles);
        // 处理循环：将angles数组扩展为 angles + [angle + 360 for angle in angles]
        List<Double> extendedAngles = new ArrayList<>(angles);
        for (double theta : angles) {
            extendedAngles.add(theta + 360);
        }

        int max = 0;
        int left = 0;
        for (int right = 0; right < extendedAngles.size(); right++) {
            while (extendedAngles.get(right) - extendedAngles.get(left) > angle) {
                left++;
            }
            max = Math.max(max, right - left + 1);
        }

        return max + same;
    }
}
